package com.houtai.controller.activity;


import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.Page;
import com.houtai.base.ResultVo;
import com.houtai.bean.dto.avtivity.TdActivityListDto;
import com.houtai.bean.dto.avtivity.comments.TdActivityCommentsInsertDto;
import com.houtai.bean.dto.avtivity.comments.TdActivityCommentsListDto;
import com.houtai.bean.vo.activity.TdActivityListVo;
import com.houtai.bean.vo.activity.UserActivityListVo;
import com.houtai.bean.vo.activity.apply.TdActivityApplyInsertDto;
import com.houtai.bean.vo.activity.comments.TdActivityCommentsListVo;
import com.houtai.service.activity.ActivityService;
import com.houtai.service.activity.apply.ActivityApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Api(value="活动接口", tags={"活动接口"})
@RequestMapping(value = {"api/v1"}, method = RequestMethod.POST)
public class ActivityController {

    @Autowired
    private ActivityService activityService;
    @Autowired
    private ActivityApplyService activityApplyService;



    @ApiOperation(value="活动列表", notes="活动列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/activity/list")
    public TdActivityListVo list(@ApiParam(value = "活动列表所需参数") @Valid @RequestBody TdActivityListDto dto, BindingResult result) {
        TdActivityListVo retInfo = new TdActivityListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityService.activityList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="活动评论", notes="活动评论")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/activity/comments")
    public ResultVo comments(@ApiParam(value = "活动评论所需参数") @Valid @RequestBody TdActivityCommentsInsertDto dto, BindingResult result,
                             @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityService.activityComments(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="活动评论列表", notes="活动评论列表")
    @Authority(AuthorityType.NoValidate)
    @RequestMapping(value = "/activity/comments/list")
    public TdActivityCommentsListVo comments(@ApiParam(value = "活动评论列表所需参数") @Valid @RequestBody TdActivityCommentsListDto dto, BindingResult result) {
        TdActivityCommentsListVo retInfo = new TdActivityCommentsListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityService.activityCommentsList(dto);
        }
        return retInfo;
    }
    @ApiOperation(value="活动报名", notes="活动报名")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/activity/apply")
    public ResultVo apply(@ApiParam(value = "活动报名所需参数") @Valid @RequestBody TdActivityApplyInsertDto dto, BindingResult result,
                          @RequestHeader String token) {
        ResultVo retInfo = new ResultVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityApplyService.activityApply(dto, token);
        }
        return retInfo;
    }
    @ApiOperation(value="我的活动列表", notes="我的活动列表")
    @Authority(AuthorityType.Login)
    @RequestMapping(value = "/user/activity/list")
    public UserActivityListVo user(@ApiParam(value = "我的活动列表所需参数") @Valid @RequestBody Page dto, BindingResult result,
                                   @RequestHeader String token) {
        UserActivityListVo retInfo = new UserActivityListVo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = activityService.userActivityList(dto, token);
        }
        return retInfo;
    }
}
